package com.ruoyi.controller.system;
import java.math.BigDecimal;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.enums.LiveStatus;
import com.ruoyi.common.enums.RuleType;
import com.ruoyi.common.enums.pay.TimeUnit;
import com.ruoyi.system.domain.SysControl;
import com.ruoyi.system.vo.SysControlRes;
import com.ruoyi.system.vo.SysWithdrawRuleRes;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysWithdrawRule;
import com.ruoyi.system.service.ISysWithdrawRuleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.thymeleaf.util.ListUtils;

/**
 * 商户代付规则Controller
 *
 * @author Ericcc
 * @date 2021-03-15
 */
@Controller
@RequestMapping("/system/withdrawrule")
public class SysWithdrawRuleController extends BaseController
{
    private String prefix = "system/withdrawrule";

    @Autowired
    private ISysWithdrawRuleService sysWithdrawRuleService;

    @RequiresPermissions("system:withdrawrule:view")
    @GetMapping()
    public String rule(ModelMap mmap)
    {
        //查询是否存在代付规则
        SysWithdrawRule search = new SysWithdrawRule();
        List<SysWithdrawRuleRes> withdrawRules = sysWithdrawRuleService.selectSysWithdrawRuleResList(search);
        if (ListUtils.isEmpty(withdrawRules)){
            //新建代付规则
            SysWithdrawRule sysWithdrawRule = new SysWithdrawRule();
            sysWithdrawRule.setMinBalance(BigDecimal.ZERO);
            sysWithdrawRule.setMaxBalance(BigDecimal.ZERO);
            sysWithdrawRule.setTodayBalance(BigDecimal.ZERO);
            sysWithdrawRule.setDeductType(LiveStatus.OK.getCode());
            sysWithdrawRule.setWithdrawRuleType(LiveStatus.OK.getCode());
            sysWithdrawRule.setWithdrawRuleStatus(LiveStatus.OK.getCode());
            String dateStr = "00:00:00";
            sysWithdrawRule.setStartTime(DateUtil.parse(dateStr));
            sysWithdrawRule.setEndTime(DateUtil.parse(dateStr));
            sysWithdrawRuleService.insertSysWithdrawRule(sysWithdrawRule);
            mmap.put("sysWithdrawRule",sysWithdrawRule);
        }else {
            SysWithdrawRuleRes sysWithdrawRule = withdrawRules.get(0);
            mmap.put("sysWithdrawRule",sysWithdrawRule);
        }
        return prefix + "/withdrawrule";
    }


    /**
     * 导出商户代付规则列表
     */
    @RequiresPermissions("system:rule:export")
    @Log(title = "商户代付规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysWithdrawRule sysWithdrawRule)
    {
        List<SysWithdrawRule> list = sysWithdrawRuleService.selectSysWithdrawRuleList(sysWithdrawRule);
        ExcelUtil<SysWithdrawRule> util = new ExcelUtil<SysWithdrawRule>(SysWithdrawRule.class);
        return util.exportExcel(list, "rule");
    }


    /**
     * 修改保存商户代付规则
     */
    @RequiresPermissions("system:rule:edit")
    @Log(title = "商户代付规则", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysWithdrawRule sysWithdrawRule)
    {
        return toAjax(sysWithdrawRuleService.updateSysWithdrawRule(sysWithdrawRule));
    }

}
